using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(NetworkIdentity))]
public class ItemSpawner : NetworkBehaviour
{
	public string PlayerTag = "Player";

	public bool SpawnByPlayerEnter = true;

	public bool SpawnJustOnce = true;

	public bool SpawnOnStart = true;

	public float timeSpawn = 120f;

	public ItemData[] Item;

	public int ItemMax = 3;

	public Vector3 Offset = new Vector3(0f, 0.1f, 0f);

	private float timeTemp;

	private List<GameObject> itemList = new List<GameObject>();

	private bool alreadySpawned;

	private bool full;

	private int ObjectsNumber;

	private void Start()
	{
		alreadySpawned = false;
		GetComponent<NetworkIdentity>().serverOnly = true;
		full = false;
	}

	public override void OnStartClient()
	{
		full = false;
		if (SpawnOnStart)
		{
			Spawn();
		}
		base.OnStartClient();
	}

	private void Spawn()
	{
		if (!base.isServer)
		{
			return;
		}
		ItemData itemData = null;
		Vector3 position = base.transform.position;
		Vector3 localScale = base.transform.localScale;
		int min = -(int)(localScale.x / 2f);
		Vector3 localScale2 = base.transform.localScale;
		float x = Random.Range(min, (int)(localScale2.x / 2f));
		Vector3 localScale3 = base.transform.localScale;
		int min2 = (int)((0f - localScale3.z) / 2f);
		Vector3 localScale4 = base.transform.localScale;
		Vector3 position2 = DetectGround(position + new Vector3(x, 0f, Random.Range(min2, (int)(localScale4.z / 2f))));
		itemData = ((Item.Length <= 0) ? UnitZ.itemManager.ItemsList[Random.Range(0, UnitZ.itemManager.ItemsList.Length)] : Item[Random.Range(0, Item.Length)]);
		if (itemData != null)
		{
			GameObject gameObject = UnitZ.gameNetwork.RequestSpawnItem(itemData.gameObject, itemData.NumTag, itemData.Quantity, position2, Quaternion.identity);
			if ((bool)gameObject)
			{
				itemList.Add(gameObject);
			}
		}
	}

	private void ObjectExistCheck()
	{
		ObjectsNumber = 0;
		foreach (GameObject item in itemList)
		{
			if (item != null)
			{
				ObjectsNumber++;
			}
		}
	}

	private void Update()
	{
		if (!base.isServer || (SpawnJustOnce && alreadySpawned))
		{
			return;
		}
		bool flag = false;
		if (SpawnByPlayerEnter)
		{
			TargetCollector targetCollector = UnitZ.aiManager.FindTargetTag(PlayerTag);
			if (targetCollector != null && targetCollector.Targets != null)
			{
				for (int i = 0; i < targetCollector.Targets.Length; i++)
				{
					if (targetCollector.Targets[i] != null)
					{
						float num = Vector3.Distance(base.transform.position, targetCollector.Targets[i].transform.position);
						Vector3 localScale = base.transform.localScale;
						if (num < localScale.x)
						{
							flag = true;
						}
					}
				}
			}
		}
		else
		{
			flag = true;
		}
		if (!flag)
		{
			return;
		}
		if (SpawnJustOnce)
		{
			if (!alreadySpawned)
			{
				alreadySpawned = true;
				int num2 = Random.Range(1, ItemMax + 1);
				for (int j = 0; j < num2; j++)
				{
					Spawn();
				}
			}
			return;
		}
		ObjectExistCheck();
		if (ObjectsNumber >= ItemMax)
		{
			full = true;
			return;
		}
		if (full)
		{
			timeTemp = Time.time;
			full = false;
		}
		if (Time.time > timeTemp + timeSpawn)
		{
			Spawn();
			timeTemp = Time.time;
		}
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.blue;
		Gizmos.DrawSphere(base.transform.position, 0.2f);
		Gizmos.DrawWireCube(base.transform.position, base.transform.localScale);
	}

	private Vector3 DetectGround(Vector3 position)
	{
		if (Physics.Raycast(position, -Vector3.up, out RaycastHit hitInfo, 1000f))
		{
			return hitInfo.point + Offset;
		}
		return position;
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
